import { Component } from '@angular/core';
import { ApiService } from '../../services/api.service';
import { AppService } from '../../services/app.service';
import { LoginInfoService } from '../../services/login-info.service';
import { UserInfoDto } from '../../models/sys/user-info.dto';
import { Consts } from '../../models/consts/consts';

@Component({
  selector: 'app-home-header',
  templateUrl: './home-header.component.html',
  styleUrl: './home-header.component.css'
})
export class HomeHeaderComponent {
  isLoggedIn = false;
  user?: UserInfoDto;
  
  constructor(private _loginInfo: LoginInfoService,
    private _api: ApiService,
    private _app: AppService) {

  }

  ngOnInit(): void {

    this._loginInfo.isLoggedIn.subscribe((value) => {
      this.isLoggedIn = value;
      console.log(`Home Header : isLoggedIn ${value}`);
      if (value) {
        this._api.getUserInfo().subscribe((resp) => {
          if (resp.code == Consts.RESP_OK && resp.data) {
            console.log(`Home Header retrieved user ${resp.data.username}`);
            this.user = resp.data;
          }
        });
      }
    });
  }

  onLogout(event: UIEvent) {
    event.preventDefault();
    this._app.logout();
  }
}
